package com.have.blog.web.controller;


import com.have.blog.base.exception.ThrowableUtils;
import com.have.blog.base.validator.group.Delete;
import com.have.blog.base.validator.group.GetOne;
import com.have.blog.base.validator.group.Insert;
import com.have.blog.base.wrapper.CommonPage;
import com.have.blog.base.wrapper.Wrapper;
import com.have.blog.web.dto.CommentDto;
import com.have.blog.web.service.CommentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RefreshScope
@RequestMapping("/web/comment")
@Api(value = "评论相关接口", tags = {"评论相关接口"})
@Slf4j
public class CommentController {
    @Autowired
    private CommentService commentService;

    @ApiOperation(value = "获取用户收到的评论回复数", notes = "获取用户收到的评论回复数")
    @GetMapping("/getUserReceiveCommentCount")
    public Wrapper getUserReceiveCommentCount(HttpServletRequest request) {
        log.info("获取用户收到的评论回复数");
        // 判断用户是否登录
//        Integer commentCount = 0;
//        if(request.getAttribute(SysConf.USER_UID) != null) {
//            String userUid = request.getAttribute(SysConf.USER_UID).toString();
//            String redisKey = RedisConf.USER_RECEIVE_COMMENT_COUNT + Constants.SYMBOL_COLON + userUid;
//            String count = redisUtil.get(redisKey);
//            if(StringUtils.isNotEmpty(count)) {
//                commentCount = Integer.valueOf(count);
//            }
//        }
        return Wrapper.success(0);
    }


    @ApiOperation(value = "获取评论列表", notes = "获取评论列表")
    @GetMapping("/getList")
    public Wrapper<CommonPage<CommentDto>> getList(@RequestParam(value = "source", name = "source", required = false) String source,
                                       @RequestParam(value = "blogUid", name = "blogUid", required = false) Integer blogUid,
                                       @RequestParam(value = "pageNum", name = "pageNum", defaultValue = "1") Integer pageNum,
                                       @RequestParam(value = "pageSize", name = "pageSize", defaultValue = "10") Integer pageSize) {

        log.info("获取文章:【{}】的评论，评论来源:{}", blogUid, source);
        return commentService.getPageList(source, blogUid, pageNum, pageSize);
    }

    @ApiOperation(value = "获取用户的评论列表和回复", notes = "获取评论列表和回复")
    @PostMapping("/getListByUser")
    public Wrapper getListByUser(@RequestParam(value = "pageNum", name = "pageNum", defaultValue = "1") Integer pageNum,
                                 @RequestParam(value = "pageSize", name = "pageSize", defaultValue = "20") Integer pageSize) {
        return commentService.getCommentByUser(pageNum, pageSize);
    }

    /**
     * 获取用户点赞信息
     *
     * @return
     */
    @ApiOperation(value = "获取用户点赞信息", notes = "增加评论")
    @PostMapping("/getPraiseListByUser")
    public Wrapper getPraiseListByUser(@ApiParam(name = "pageNum", value = "当前页数", required = false) @RequestParam(name = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                                       @ApiParam(name = "pageSize", value = "每页显示数目", required = false) @RequestParam(name = "pageSize", required = false, defaultValue = "10") Integer pageSize) {

        return commentService.getPraiseListByUser(pageNum, pageSize);
    }

    @ApiOperation(value = "增加评论", notes = "增加评论")
    @PostMapping("/add")
    public Wrapper add(@Validated({Insert.class}) @RequestBody CommentDto commentDto, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        return commentService.addComment(commentDto);
    }

//    @BussinessLog(value = "删除评论", behavior = EBehavior.DELETE_COMMENT)
    @ApiOperation(value = "删除评论", notes = "删除评论")
    @PostMapping("/delete")
    public Wrapper deleteComments(@Validated({Delete.class})@RequestBody CommentDto commentDto, BindingResult result) {
        ThrowableUtils.checkParamArgument(result);
        return commentService.deleteComments(commentDto);
    }

//    @BussinessLog(value = "举报评论", behavior = EBehavior.REPORT_COMMENT)
    @ApiOperation(value = "举报评论", notes = "举报评论")
    @PostMapping("/report")
    public Wrapper reportComment(@Validated({GetOne.class}) @RequestBody CommentDto commentDto, BindingResult result) {

        ThrowableUtils.checkParamArgument(result);
        return commentService.reportComment(commentDto);
    }
}